package com.beeantec.jnrerc.modules.comment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;
import com.beeantec.jnrerc.dto.CommentVo;
import com.beeantec.jnrerc.dto.request.CommentListReqDTO;
import com.beeantec.jnrerc.dto.request.CommentReqDTO;
import com.beeantec.jnrerc.entity.Comment;
import com.beeantec.jnrerc.entity.Member;
import com.beeantec.jnrerc.modules.comment.service.CommentService;

import com.beeantec.jnrerc.modules.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Api(value = "用户评论", tags = "用户评论")
@RestController
@RequestMapping(value = "/api/comment")
public class CommentController extends BaseController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private MemberService memberService;


    @ApiOperation(value = "获取评论列表接口", notes = "获取评论列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ApiRest<List<CommentVo>> paging(@RequestParam("page") Integer page, @RequestParam("size") Integer size,@RequestParam("contentId") Integer contentId,@RequestParam("type") Integer type) {
        //分页查询并转换
       IPage<Comment> pageComment = commentService.page(page,size,contentId,type);
       List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment:pageComment.getRecords()){
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(comment, commentVo);
            Member member= memberService.getById(comment.getMemberId());
            if (member != null) {
                if (comment.getHide()==0){
                    commentVo.setNickName(member.getNickName());
                }else {
                    commentVo.setNickName("匿名用户");
                }
                commentVo.setAvatarUrl(member.getAvatarUrl());
                commentVoList.add(commentVo);
            }

        }
        return success(commentVoList);

    }

    @ApiOperation(value = "添加评论接口", notes = "添加评论接口")
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public ApiRest<Boolean> add(@RequestBody CommentReqDTO commentReqDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentReqDTO,comment);
        comment.setCreateTime(new Date());
        if (commentService.add(comment)){
            return success();
        }
        return failure();
    }

    @RequestMapping(value = "/amount/{answerId}-{answerType}",method = RequestMethod.GET)
    public ApiRest<Map<String, Integer>> amount(@PathVariable @NotNull(message = "answerId required") String answerId, @PathVariable @NotNull(message = "answerType required") Integer answerType) {
        return success(commentService.count(answerId, answerType));
    }




}
